<?php 
	namespace Home\Model;
	use Hdphp\Model\Model;
	class Users extends Model{
		protected $validate=array(
				array('username','required','用户名不能为空',3,3),
				array('passwd','required','密码不能为空',3,3)
			);
		protected $auto=array(
				array('login_time','time','function',3,3)
			);

		//登录后的操作，更新指定用户的登录ip及登录时间
		public function afterLogin($uid){
			$ip=Request::ip();
			$time=time();
			$this->where('uid',$uid)->update(array('login_ip'=>$ip,'login_time'=>$time));
			return true;
		}

		public function addData(){
			if(!$this->create())return false;
			$this->passwd=md5($this->passwd);
			return $this->add();
		}

		public function editUser(){
			$passwd=q('post.passwd');
			if(empty($passwd)){
				unset($_POST['passwd']);
			}else{
				$_POST['passwd']=md5($_POST['passwd']);
			}
			return $this->save($_POST);
		}
	}
?>